@extends('backstage.layout.master')
@section('title', '产品列表')
@section('style')
    <link rel="stylesheet" href="css/order.css"/>
    <style>
        a{
            cursor: pointer;
        }
        .in-line{
            float: right;
        }
    </style>
@stop
@section('content')
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>

        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a href="backstage/home">主页</a>
            </li>

            <li class="active">
                <a href="backstage/products/list/1">产品列表</a>
            </li>
        </ul><!-- .breadcrumb -->
    </div>

    <div class="page-content">
        <div class="page-header">
            <h1>
                产品列表
            </h1>
        </div>
        <div class="col-sm-12">
            <div class="well">
                <select name="product_temp_category_id" id="product-temp-category">
                    @foreach($product_temp_categorys as $product_temp_category)
                        <option @if(((isset($conditions->product_temp_category_id) && $conditions->product_temp_category_id == $product_temp_category->product_temp_category_id))) selected @endif value="{{$product_temp_category->product_temp_category_id}}">{{$product_temp_category->name}}</option>
                    @endforeach
                </select>
                <select name="user_type" id="user_type">
                    <option value="0" @if(isset($conditions->user_type) && $conditions->user_type == '0') selected @endif>全部</option>
                    <option value="1" @if(isset($conditions->user_type) && $conditions->user_type  == '1') selected @endif>设计师</option>
                    <option value="2" @if(isset($conditions->user_type) && $conditions->user_type  == '2') selected @endif>品牌商</option>
                </select>
                <select name="brand_id" id="brand_id">
                    <option value="0" @if(isset($conditions->brand_id) && $conditions->brand_id == '0') selected @endif>全部</option>
                    @foreach($brands as $brand)
                        <option value="{{$brand->brand_id}}" @if(isset($conditions->brand_id) && $conditions->brand_id == $brand->brand_id) selected @endif>{{$brand->brand_name}}</option>
                    @endforeach
                </select>
                <select name="is_shelves" id="is_shelves">
                    <option value="-1" @if(isset($conditions->is_shelves) && $conditions->is_shelves  == '-1') selected @endif>全部</option>
                    <option value="1" @if(isset($conditions->is_shelves) && $conditions->is_shelves  == '1') selected @endif>上架</option>
                    <option value="0" @if(isset($conditions->is_shelves) && $conditions->is_shelves  == '0') selected @endif>下架</option>
                </select>
                <input type="text" class="keyword1" name="user_param" placeholder="用户昵称/手机号" value="{{isset($conditions->user_param) ? $conditions->user_param : ''}}">
                <input type="text" class="keyword" name="product_id" placeholder="产品ID" value="{{isset($conditions->product_id) ? $conditions->product_id : ''}}">
                <input type="button" value=" 搜索 " class="button_search">
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                <div class="state">
                    <ul>
                        <li @if($cur == 1)class="cur"@endif><a href="backstage/products/list/1">全部</a></li>
                        <li @if($cur == 2)class="cur"@endif><a href="backstage/products/list/2">上架产品</a></li>
                        <li @if($cur == 3)class="cur"@endif><a href="backstage/products/list/3">设计师作品</a></li>
                        <li @if($cur == 4)class="cur"@endif><a href="backstage/products/list/4">品牌商作品</a></li>
                        <li @if($cur == 5)class="cur"@endif><a href="backstage/products/list/5">搜索结果</a></li>
                    </ul>
                    <div class="curBg"></div>
                    <div class="cls"></div>
                </div>
                {{--<select class="in-line" onchange="window.location.href='backstage/products/list/'">--}}
                    {{--<option value="created_at">最新</option>--}}
                    {{--<option value="collect_num">人气</option>--}}
                {{--</select>--}}
                <div class="table-responsive">
                    <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                        <thead>
                        <tr>
                            <th>产品编号</th>
                            <th>产品预览图</th>
                            <th>已售数量</th>
                            <th>备注</th>
                            <th>产品状态</th>
                            <th>创建时间</th>
                            <th>显示状态</th>
                            <th>渠道</th>
                            <th>操作</th>
                        </tr>
                        </thead>

                        <tbody>
                        @foreach ($products as $product)
                        <tr>
                            <td>{{$product->product_id}}</td>
                            <td>
                                @if(count($product->thumbnail) > 0)
                                    <img src="{{$product->thumbnail[0]}}" style="width: 120px;"/>
                                @endif
                            </td>
                            <td>{{$product->sell_num}}</td>
                            <td>
                                {{$product->remarks}}
                            </td>
                            <td>
                                @if($product->is_shelves == 1)
                                    <a class="blue">已上架</a>
                                    @else
                                    <a class="red">未上架</a>
                                @endif
                            </td>
                            <td>{{date('Y-m-d H:i:s',$product->created_at)}}</td>
                            <td>
                                <select name="is_show" class="is-show" data="{{$product->product_id}}">
                                    <option value="1" @if($product->is_show==1) selected @endif>显示</option>
                                    <option value="0" @if($product->is_show==0) selected @endif>隐藏</option>
                                </select>
                            </td>
                            <th>{{$product->channel_name}}</th>
                            <td>
                                <a class="blue" href="backstage/products/detail/{{$product->product_id}}">详情</a>
                                <a class="blue" href="javascript:void(0);" onclick="rebuildPrintImg('{{isset($product->product_id)?$product->product_id:""}}')">重新生成打印图</a>
                            </td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="dataTables_info" id="sample-table-2_info"></div>
                    </div>
                    <div class="col-sm-6">
                        <div class="dataTables_paginate paging_bootstrap">
                            @if(empty($conditions))
                                <?php echo $products->render(); ?>
                            @else
                                <?php echo $products->appends(['conditions' => json_encode($conditions)])->render(); ?>
                            @endif

                        </div>
                    </div>
                </div>
                            <!-- PAGE CONTENT ENDS -->
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.page-content -->
@stop
@section('script')
    <script type="text/javascript">
        var _token = '{{csrf_token()}}';
        var flag = 1;
        $(function(){
            var $liCur = $(".state ul li.cur"),
                    curP = $liCur.position().left,
                    curW = $liCur.outerWidth(true),
                    $slider = $(".curBg"),
                    $navBox = $(".state");
            $targetEle = $(".state ul li a"),
                    $slider.animate({
                        "left":curP,
                        "width":curW
                    });
            $targetEle.mouseenter(function () {
                var $_parent = $(this).parent(),
                        _width = $_parent.outerWidth(true),
                        posL = $_parent.position().left;
                $slider.stop(true, true).animate({
                    "left":posL,
                    "width":_width
                }, "fast");
            });
            $navBox.mouseleave(function (cur, wid) {
                cur = curP;
                wid = curW;
                $slider.stop(true, true).animate({
                    "left":cur,
                    "width":wid
                }, "fast");
            });
            $(".is-show").change(function(){
                var product_id = $(this).attr("data");
                var is_show = $(this).val();
                var element = $(this);
                if (flag == 1) {
                    flag = 0;
                    $.ajax({
                        type: "POST",
                        url: "backstage/products/show",
                        data: {"is_show":is_show,"_token":_token,"product_id": product_id},
                        success: function (msg) {
                            flag = 1;
                            if (msg.state == "M00000") {
                                element.val(msg.result.is_show);
                                showMessage("",msg.message);
                            } else {
                                showMessage(msg.message, "");
                            }
                        },
                        error: function () {
                            flag = 1;
                            showMessage("修改失败!", "");
                        }
                    });
                }
            });
            $(".button_search").click(function(){
                var user_type = $("#user_type option:selected").val();
                var brand_id = $("#brand_id option:selected").val();
                var is_shelves = $("#is_shelves option:selected").val();
                var user_param = $(".keyword1").val();
                var product_id = $(".keyword").val();
                var product_temp_category_id = $("#product-temp-category option:selected").val();
                search(user_type,brand_id,is_shelves,user_param,product_id,product_temp_category_id)
            });
        });
        var state = 0;
        /***
         * 重新生成打印图
         * @param product_id
         */
        function rebuildPrintImg(product_id) {
            if(state == 0){
                state = 1;
                if(product_id ==''){
                    swal('产品Id获取出错～','','warning');
                    state = 0;
                    return ;
                }
                swal('处理中，请稍后','','warning');
                $.ajax({
                    type: "post",
                    url: "/backstage/products/rebuild-print-img",
                    data:{"product_id":product_id},
                    dataType: 'json',
                    success: function(res){
                        if(res.state ==1){
                            swal({title:'更新！', text: "", type: "success", timer:500});
                            return ;
                        }
                        swal(res.message,'','warning');
                    },
                    error:function(){
                        swal('系统繁忙','请稍后重试','warning');
                    }
                });
                state = 0;
            }
        }
        function search(user_type,brand_id,is_shelves,user_param,product_id,product_temp_category_id){
            var json = '{ "user_type" : "'+ user_type +'", "brand_id" : "'+ brand_id +'", "is_shelves" : "'+is_shelves+'", "user_param" : "'+ user_param +'", "product_id" : "'+product_id+'", "product_temp_category_id" : "'+product_temp_category_id+'"}';
            console.log(json);
            window.location.href = "backstage/products/list/5/?conditions="+json;
        }
    </script>
@stop